package 品牌管理;

import java.sql.*;
import java.util.ArrayList;

public class 查询全部品牌 {
    public static void main(String[] args) {
//        String sql = "SELECT * FROM t_brand";
//        1.加载驱动com.mysql.jdbc.Driver
//        创建list存放品牌
//        泛型
        ArrayList<brand> brandList =  new ArrayList<brand>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
//        2.使用驱动管理器（DriverManager）连接数据库              ？是后面要加参数&支持中文&xx连接关闭&服务器通用时区
            String url = "jdbc:mysql://10.4.43.109/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
            String user = "root";
            String pass = "000000";
            conn = DriverManager.getConnection(url, user, pass);
//        3.书写sql语句并执行
            String sql = "SELECT * FROM t_brand";
//        4.创建执行SQL的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
//        5.从rs中取出结果
            while (rs.next()){
//                rs.getString(1);
                int id = rs.getInt("id");
                String brandName = rs.getString("brand_name");
                String companyname = rs.getString("company_name");
                int ordered = rs.getInt("ordered");
                String description = rs.getString("description");
                int status = rs.getInt("status");
//                System.out.println(id+brandName+companyname+ordered+description+status);
//                封装
                brand b = new brand(id,brandName,companyname,
                        ordered,description,status);
                brandList.add(b);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //        6.关闭连接
            try {
                rs.close();
                stmt.close();
                conn.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        for (brand brand : brandList){
            System.out.println(brand);
        }


    }
}
